package com.zjkj.mp.controller.messageManage;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.zjkj.mp.service.message.MessageService;
import com.zjkj.mp.service.user.UserService;
import com.zjkj.mp.utils.PaginatedResult;
import com.zjkj.mp.vo.message.MessageState;

/**
 * 
 * 审核短信控制器
 *
 * @author 陈晨
 * @history
 * <TABLE id="HistoryTable" border="1">
 * 	<TR><TD>时间</TD><TD>描述</TD><TD>作者</TD></TR>
 *	<TR><TD>2013-6-9</TD><TD>创建初始版本</TD><TD>陈晨</TD></TR>
 * </TABLE>
 */
@Controller
public class SHDXController {
	private SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
	private SimpleDateFormat sdf2 = new SimpleDateFormat("yyyyMMdd");
	
	private int pageSize = 8;
	@Resource(name = "userServiceImpl")
	private UserService userService;
	@Autowired
	private MessageService messageService;
	

	/******************************* 审核短信 ***********************************/
	/**
	 * 跳转至审核页面
	 * @return
	 * @throws ParseException 
	 */
	@RequestMapping("/validMessage.do")
	public String validMessage(HttpServletRequest req) throws ParseException {
		String pageNoStr = req.getParameter("pageNo");		
		String userName = req.getParameter("userName");
		String beginDate = req.getParameter("beginDate");
		String endDate = req.getParameter("endDate"); 
		 
		//设置参数
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("state",MessageState.NEW);
		params.put("state1",MessageState.AUDIT_OK);
		if(pageNoStr == null) pageNoStr = "1";
		if(!"".equals(userName)) params.put("userName", userName);
		if(!"".equals(beginDate) && beginDate != null) params.put("beginDate", getDate(beginDate)+"000000" );
		if(!"".equals(endDate)  && endDate != null) params.put("endDate", getDate(beginDate) + "235959");
		
		PaginatedResult result = this.messageService.getMessageList(Integer.parseInt(pageNoStr), pageSize, params);
		
		req.setAttribute("beginDate", beginDate);
		req.setAttribute("endDate", endDate);
		req.setAttribute("userName",userName);
		req.setAttribute("result", result);		
		return "message/validMessage";
	}	
	
	/**
	 * 短信审核通过
	 * @throws ParseException 
	 */
	@RequestMapping("/auditMessage.do")
	public String auditMessage(HttpServletRequest req) throws ParseException{
		String fsId = req.getParameter("fsId");
		this.messageService.updateMessageState(fsId, MessageState.AUDIT_OK);
		
		//返回列表
		return this.validMessage(req);
	}
	
	
	/**
	 * 删除短信，标记为审核不通过
	 * @throws ParseException 
	 */
	@RequestMapping("/deleteMessage.do")
	public String deleteMessage(HttpServletRequest req) throws ParseException{
		String fsId = req.getParameter("fsId");
		this.messageService.updateMessageState(fsId, MessageState.AUDIT_NO);
		
		//返回列表
		return this.validMessage(req);
	}
	
	private String getDate(String date) throws ParseException{
		Date  t1 = sdf1.parse(date);
		String result = sdf2.format(t1);
		return result;
	}
	 
}